﻿using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace PMP.Core.Extensions;

public class JsonObjectToSimpleTypeBinderProvider: IModelBinderProvider
{
    public IModelBinder? GetBinder(ModelBinderProviderContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        // 仅对简单类型（string、int、long 等）生效
        if (!context.Metadata.IsComplexType && 
            context.Metadata.ModelType != typeof(IFormFile) && // 排除文件类型
            context.Metadata.ModelType != typeof(IFormFileCollection))
        {
            // 直接传入 fallbackBinder 而不是通过 DI 解析
            var fallbackBinder = context.CreateBinder(context.Metadata);
            return new JsonObjectToSimpleTypeBinder(fallbackBinder);
        }

        return null;
    }
}